package com.deryun.tyl.controller;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.deryun.tyl.bean.User;
import com.deryun.tyl.service.impl.UserServiceImpl;

@Controller
@RequestMapping("accountAuthentication")
public class AccountAuthenticationController
{
	@Resource(name="userService")
	private UserServiceImpl userSI;
	
	/*首页*/
	@RequestMapping("/saveMassage")
	public String saveMassage(
			@RequestParam(value = "frontOfID", required = false) String frontOfID,
			@RequestParam(value = "frontOfIDCard", required = false) MultipartFile frontOfIDCard,
			@RequestParam(value = "bardOfIDCard", required = false) MultipartFile bardOfIDCard,
			@RequestParam(value = "personalAvatar", required = false) MultipartFile personalAvatar,
			@RequestParam(value = "password", required = false) String password,
			@RequestParam(value = "contactMan", required = false) String contactMan,
			@RequestParam(value = "contactPhone", required = false) String contactPhone,
			HttpServletRequest request) throws Exception
	{		
		System.out.println("开始");  
		String path = "/virtualhost/jbwz/ROOT/upload/image";  
		User user = (User) request.getSession().getAttribute("user");
		if (!frontOfIDCard.isEmpty()) {
			String frontOfIDCardFileName = String.valueOf(System.currentTimeMillis()) + ".png";
			File frontOfIDCardFile = new File(path, frontOfIDCardFileName);  
			if(!frontOfIDCardFile.exists())
			{  
				frontOfIDCardFile.mkdirs();  
			}  
			frontOfIDCard.transferTo(frontOfIDCardFile);  
			user.setCardPicFront("upload/image/"+frontOfIDCardFileName);
		}
		if (!bardOfIDCard.isEmpty()) {
			String bardOfIDCardFileName = String.valueOf(System.currentTimeMillis()) + ".png";
			File bardOfIDCardFile = new File(path, bardOfIDCardFileName);
			if(!bardOfIDCardFile.exists())
			{  
				bardOfIDCardFile.mkdirs();  
			}
			bardOfIDCard.transferTo(bardOfIDCardFile);  
			user.setCardPicBack("upload/image/"+bardOfIDCardFileName);
		}
		if (!personalAvatar.isEmpty()) {
			String personalAvatarFileName = String.valueOf(System.currentTimeMillis()) + ".png"; 
			File personalAvatarFile = new File(path, personalAvatarFileName);
			if(!personalAvatarFile.exists())
			{  
				personalAvatarFile.mkdirs();  
			}
			personalAvatar.transferTo(personalAvatarFile);  
			user.setHeadPic("upload/image/"+personalAvatarFileName);
		}
		if (!frontOfID.isEmpty()) {
			user.setIdCardNum(frontOfID);
		}
		if (!password.isEmpty()) {
			user.setPassword(password);
		}
		if (!contactMan.isEmpty()) {
			user.setContactMan(contactMan);
		}
		if (!contactPhone.isEmpty()) {
			user.setContactPhone(contactPhone);
		}
		
		userSI.modifyUser(user);
		return "redirect:/user/toMyAuthStat";
	}
	
}